package CH01数组结构.LeetCode双指针;

public class LC1013 {
    public boolean canThreePartsEqualSum(int[] arr) {
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        if (sum % 3 != 0) {
            return false;
        }
        int key = sum / 3;
        int l = 0;
        int r = arr.length - 1;
        int leftSum = arr[l];
        int rightSum = arr[r];
        while (l + 1 < r) {
            if (leftSum == key && rightSum == key) {
                return true;
            }
            if (leftSum != key) {
                leftSum += arr[++l];
            }
            if (rightSum != key) {
                rightSum += arr[--r];
            }
        }
        return false;
    }
}